package com.example.android.common.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.cardemulation.AccountStorage;
import com.example.android.cardemulation.MainActivity;
import com.example.android.cardemulation.R;
import com.example.android.common.http.ThreadPoolUtils;
import com.example.android.common.logger.Log;
import com.example.android.common.utils.MD5Utils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Joe on 2016/5/31.
 */
public class BindActivity extends Activity {
    private EditText etCardNumber;
    private TextView tvAgreement;
    private Button btnAgree;
    private Button btnDisagree;
    private static final String TAG = "BindActivity";
    private static final int FLAG_BIND=1;
    private String token;
    Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what) {
                case FLAG_BIND:

            }
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
        initView();
    }

    public void agree(View view){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    public void disagree(View view){

    }

    public void bind(View view){
        String cardnum = etCardNumber.getText().toString().replaceAll(" ","");
        //CardNum传递给银行，获取token~~~
        Pattern pattern = Pattern.compile("^[0-9]{18}[0-9]?[0-9]?[0-9]?$");
        Matcher matcher = pattern.matcher(cardnum);
        if(matcher.matches()) {
            token = MD5Utils.MD5(cardnum);
            AccountStorage.setAccount(this, "token", token);

            Log.i(TAG,"Token :"+token);
            tvAgreement.setVisibility(View.VISIBLE);
            btnAgree.setVisibility(View.VISIBLE);
            btnDisagree.setVisibility(View.VISIBLE);
        }else{
            Toast.makeText(this,this.getResources().getString(R.string.notice_warning_wrong_card_num),Toast.LENGTH_SHORT).show();
        }
    }

    private void initView(){
        etCardNumber = (EditText) findViewById(R.id.bind_card_number);
        tvAgreement = (TextView) findViewById(R.id.bind_tv_agreement);
        btnAgree = (Button) findViewById(R.id.bind_btn_agree);
        btnDisagree = (Button) findViewById(R.id.bind_btn_disagree);
    }
}